package com.zzzy.server.security;

import com.zzzy.common.util.JsonUtil;
import com.zzzy.common.util.ServletUtils;
import com.zzzy.common.vo.AjaxResult;
import com.zzzy.server.common.cache.LoginUserCache;
import com.zzzy.server.service.TokenService;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Description: 自定义退出处理类 返回成功
 *
 * @author fc
 * @date 2024/4/12
 */
@Service
public class LogoutSuccessHandlerImpl implements LogoutSuccessHandler {

    @Resource
    private LoginUserCache loginUserCache;
    @Resource
    private TokenService tokenService;

    @Override
    public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) {
        loginUserCache.removeLoginUser(tokenService.getToken());
        ServletUtils.renderString(response, JsonUtil.toJSONString(AjaxResult.success()));
    }
}
